package Power;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URLClassLoader;

import javax.imageio.ImageIO;

import Main.SpaceGame;
import World.TileType;

public class PowerConnector extends PowerNode
{
	private SpaceGame main;
	BufferedImage sprite = null;
	
	public PowerConnector(int x, int y, SpaceGame main)
	{
		super(x,y,main);
		this.main = main;
		powerRadius = 10;
		this.x = x;
		this.y = y;
		
		try {
			sprite = ImageIO.read(URLClassLoader.getSystemResource("Resources/"+"PowerConnector.png"));
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		setText();
	}
	
	public void draw(Graphics g) 
	{
		g.drawImage(sprite, x*10, y*10, null);
		drawFlash(g, x, y);
	}

	public void update() 
	{
		checkPowered(x,y);
		if (powered)
		{
			for(int dx = x-powerRadius;dx <= x+powerRadius;dx++)
			{
				for(int dy = y-powerRadius;dy <= y+powerRadius;dy++)
				{
					if (Math.pow(dx - x-.5,2) + Math.pow(dy - y-.5, 2) <= Math.pow(powerRadius,2) && main.world.tiles[dx][dy].getType() == TileType.Interior)
					{
						if (!(dx == x && dy == y))
							main.world.tiles[dx][dy].setPowered(true);
					}
				}
			}
		}
		text.clear();
		setText();
	}

	@Override
	public void keyPressed(KeyEvent arg0) 
	{
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) 
	{
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) 
	{
		
	}

	@Override
	public void setText() 
	{
		textColorIndex = 2;
		
		colors.add(Color.cyan);
		colors.add(Color.cyan);
		text.add("");
		text.add(getName());
		
		colors.add(Color.cyan);
		colors.add(Color.cyan);
		text.add("");
		text.add("");
		
		if(powered)
		{
			
		}
		
		else
		{
			colors.add(Color.red);
			colors.add(Color.red);
			text.add("Needs Power");
			text.add("");
		}

	}

	@Override
	public String getName() 
	{
		return "Power Connector";
	}
}
